<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class GbController extends Controller
{
    //商标图片管理 add
    public function getAdd()
    {
    	$data = DB::table('cate')
            -> select(['id', 'name', 'path', DB::raw('CONCAT(path, ",", id) AS sortPath')])
            ->orderBy('sortPath', 'ASC')
            // -> orderBy(DB::raw('CAST("sortPath" AS SIGNED)'),'ASC')
            -> get();

            //dd($data);

        
        foreach($data as $k => $v)
        {
            $count = substr_count($v -> path, ',');
            $str = str_repeat('|----',$count);
            $data[$k] -> c = substr_count($v -> path, ',');
            $data[$k] -> name = $str.$v -> name;

        }

    	return view('admin.goodsbrands.add',['data' => $data]);
    }

    //商标图片管理 insert
    public function postInsert(Request $request)
    {
    	//dd($request -> all());
    	$data = $request -> except('_token');

    	//商标图片
    	if($request -> hasFile('goodsbrands'))
    	{
    		if($request -> file('goodsbrands') -> isValid())
    		{
    			//获取扩展名称
    			$suffix = $request -> file('goodsbrands') -> getClientOriginalExtension();

    			//随机名称
    			$fileName =time().mt_rand(100000,999999).'.'.$suffix;

    			// echo $fileName;
    			
    			$request -> file('goodsbrands') -> move('./uploads/goods/imgs/', $fileName);

    			//处理 img字段
		    	$data['goodsbrands'] = '/uploads/goods/imgs/'.$fileName;
    		}
    	}

        //这里加个时间是为了编辑或删除时的唯一性 现加上再说
    	$data['goodsbrandtime'] = date('Y-m-d H:i:s',time());

    	//dd($data);
    	$res = DB::table('goods_brand') -> insert($data);

    	if($res)
    	{
    		return redirect('/admin/goodsbrands/index') -> with(['info' => '插入成功']);
    	}else
    	{
    		return back() -> with(['info' => '插入失败']);
    	}
    }

    //商标图片管理 index
    public function getIndex()
    {
    	$data = DB::table('goods_brand') -> get();
    	return view('admin.goodsbrands.index',['data' => $data]);
    }

    //商标图片管理 edit
    public function getEdit($id)
    {
    	$data = DB::table('goods_brand') -> where('id', $id) -> first();

    	return view('admin.goodsbrands.edit', ['data' => $data]);
    }

    //商标图片管理 update
    public function postUpdate(Request $request)
    {
    	//dd($request -> all());
    	$id = $request -> input('id');

    	$oldGoodsbrands = DB::table('goods_brand') -> where('id', $id) -> first() -> goodsbrands;

    	$data = $request -> except('_token','id');

    	if($request -> hasFile('goodsbrands')) 
    	{
    		if($request -> file('goodsbrands') -> isValid())
    		{
    			//获取扩展名称
	    		$suffix = $request -> file('goodsbrands') -> getClientOriginalExtension();
	    			
	    		//随机名称
	    		$fileName = time().mt_rand(100000,999999).'.'.$suffix;
	    		//dd($fileName);
	    		$request -> file('goodsbrands') -> move('./uploads/goods/', $fileName);

	    		//处理 img 字段
	    		$data['goodsbrands'] = '/uploads/goods/'.$fileName;
	    		
	    		if(file_exists('.'.$oldGoodsbrands))
	    		{
	    			unlink('.'.$oldGoodsbrands);
	    		}

    		}
    		
    	}

    	//更新

    	$res = DB::table('goods_brand') -> where('id', $id) -> update($data);

	    //判断
	    if($res)
	    {
	    	return redirect('/admin/goodsbrands/index') -> with(['info' => '更新成功']);
	    }else
	    {
	    	return back() -> with(['info' => '更新失败']);
	    }

    }

    //商标图片 edit
    public function getDelete($id)
    {
    	$res = DB::table('goods_brand') -> where('id', $id) -> delete();

    	if($res)
        {
            return back() -> with(['info' => '删除成功']);
        }else
        {
            return back() -> with(['error' => '删除失败']);
        }
    }
}
